
/*
 * Copyright 2002-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2023原作者。
 *
 *根据Apache许可证2.0版许可（“许可证”）；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本：
 *
 *https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证进行的分发是在“按原样”的基础上进行的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证和
 *许可证下的限制。
 */

package org.springframework.scheduling.annotation;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncExecutionInterceptor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;

/**
 * Specialization of {@link AsyncExecutionInterceptor} that delegates method execution to
 * an {@code Executor} based on the {@link Async} annotation.
 *
 * <p>Specifically designed to support use of the {@link Async#value()} executor
 * qualifier mechanism.
 *
 * <p>Supports detecting qualifier metadata via {@code @Async} at the method or
 * declaring class level. See {@link #getExecutorQualifier(Method)} for details.
 *
 * @author Chris Beams
 * @author Stephane Nicoll
 * @since 3.1.2
 * @see org.springframework.scheduling.annotation.Async
 * @see org.springframework.scheduling.annotation.AsyncAnnotationAdvisor
 */
/**
 *将方法执行委托给的｛@link AsyncExecutionInterceptor｝的专业化
 *基于｛@link Async｝注释的｛@code Executor｝。
 *
 *<p>专门设计用于支持使用{@link Async#value（）}执行器
 *限定符机制。
 *
 *＜p＞支持在方法或处通过｛@code@Async｝检测限定符元数据
 *声明类级别。有关详细信息，请参阅｛@link#getExecutiorQualifier（Method）｝。
 *
 *@作者Chris Beams
 *@作者Stephane Nicoll
 *@自3.1.2起
 *@参见org.springframework.scheduling.annotation.Async
 *@参见org.springframework.scheduling.annotation.AsyncAnnotationAddvisor
 */
public class AnnotationAsyncExecutionInterceptor extends AsyncExecutionInterceptor {

	/**
	 * Create a new {@code AnnotationAsyncExecutionInterceptor} with the given executor
	 * and a simple {@link AsyncUncaughtExceptionHandler}.
	 * @param defaultExecutor the executor to be used by default if no more specific
	 * executor has been qualified at the method level using {@link Async#value()};
	 * a local executor for this interceptor will be built otherwise
	 */
	/**
	 *使用给定的执行器创建一个新的｛@code AnnotationSyncExecutionInterceptor｝
	 *和一个简单的{@link异步捕获异常处理程序}。
	 *@param defaultExecutor如果没有更具体的，则默认使用的执行器
	 *executor已在方法级别使用{@link Async#value（）}进行了限定；
	 *否则将生成此拦截器的本地执行器
	 */
	public AnnotationAsyncExecutionInterceptor(@Nullable Executor defaultExecutor) {
		super(defaultExecutor);
	}

	/**
	 * Create a new {@code AnnotationAsyncExecutionInterceptor} with the given executor.
	 * @param defaultExecutor the executor to be used by default if no more specific
	 * executor has been qualified at the method level using {@link Async#value()};
	 * a local executor for this interceptor will be built otherwise
	 * @param exceptionHandler the {@link AsyncUncaughtExceptionHandler} to use to
	 * handle exceptions thrown by asynchronous method executions with {@code void}
	 * return type
	 */
	/**
	 *使用给定的执行器创建一个新的｛@code AnnotationSyncExecutionInterceptor｝。
	 *@param defaultExecutor如果没有更具体的，则默认使用的执行器
	 *executor已在方法级别使用{@link Async#value（）}进行了限定；
	 *否则将生成此拦截器的本地执行器
	 *@param exceptionHandler要用于
	 *使用｛@code void｝处理异步方法执行引发的异常
	 *返回类型
	 */
	public AnnotationAsyncExecutionInterceptor(@Nullable Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler) {
		super(defaultExecutor, exceptionHandler);
	}


	/**
	 * Return the qualifier or bean name of the executor to be used when executing the
	 * given method, specified via {@link Async#value} at the method or declaring
	 * class level. If {@code @Async} is specified at both the method and class level, the
	 * method's {@code value} takes precedence (even if empty string, indicating that
	 * the default executor should be used preferentially).
	 * @param method the method to inspect for executor qualifier metadata
	 * @return the qualifier if specified, otherwise an empty string indicating that the
	 * {@linkplain #setExecutor(Executor) default executor} should be used
	 * @see #determineAsyncExecutor(Method)
	 */
	/**
	 *返回要在执行
	 *给定的方法，通过方法或声明处的｛@link Async#值｝指定
	 *班级级别。如果在方法和类级别都指定了｛@code@Async｝，则
	 *方法的{@code值}优先（即使是空字符串，也表示
	 *应该优先使用默认的执行器）。
	 *@param方法用于检查executor限定符元数据的方法
	 *@如果指定，则返回限定符，否则为空字符串，指示
	 *应使用｛@linkplain#setExecutior（Executior）default Executor｝
	 *@参见#determineSyncExecutor（方法）
	 */
	@Override
	@Nullable
	protected String getExecutorQualifier(Method method) {
		// Maintainer's note: changes made here should also be made in
		// 维护人员注意：此处所做的更改也应在
		// AnnotationAsyncExecutionAspect#getExecutorQualifier
		// AnnotationSyncExecutionAspect#getExecutiorQualifier注释同步执行特性
		Async async = AnnotatedElementUtils.findMergedAnnotation(method, Async.class);
		if (async == null) {
			async = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), Async.class);
		}
		return (async != null ? async.value() : null);
	}

}
